<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请开始时间" prop="startDate" label-width="100">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请结束时间" prop="endDate" label-width="100">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请人" prop="applicantName">
        <el-input
          v-model="queryParams.applicantName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批单号" prop="auditNo">
        <el-input
          v-model="queryParams.auditNo"
          placeholder="请输入审批单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员卡号" prop="memberCardNumber">
        <el-input
          v-model="queryParams.memberCardNumber"
          placeholder="请输入会员卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item  label="审批状态" prop="auditStatus">
        <el-select
          @change="handleQuery"
          v-model="queryParams.auditStatus"
          placeholder="请选择审批状态"
        >
          <el-option
            v-for="dict in sys_approval_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"

          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
<!--        <el-col :span="1.5">-->
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['business:bokaRefundOrder:export']"
          >导出</el-button>
<!--        </el-col>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="Plus"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:bokaRefundOrder:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:bokaRefundOrder:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:bokaRefundOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bokaRefundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="申请人" align="center" prop="applicantName" />
      <el-table-column label="复核提点" align="center" prop="reviewedBonus" />
<!--      <el-table-column label="申请人id" align="center" prop="applicantId" />-->
      <el-table-column label="军区" align="center" prop="militaryArea" />
      <el-table-column label="门店" align="center" prop="shopName" />
      <el-table-column label="老师名字" align="center" prop="teacherName" />
      <el-table-column label="客户名字" align="center" prop="memberName" />
      <el-table-column label="会员卡号" align="center" prop="memberCardNumber" />
      <el-table-column label="品种分类" align="center" prop="varietyClassification" />
      <el-table-column label="商品分类" align="center" prop="productCategory" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品价格" align="center" prop="productPrice" />
      <el-table-column label="开单时间" align="center" prop="billDate" width="180" />
      <el-table-column label="退款原因" align="center" prop="refundReason" />
      <el-table-column label="单据照片" align="center" prop="billImg" width="180" >
        <template #default="scope">
          <template v-if="scope.row.billImg != null">
            <template
              v-for="item in scope.row.billImg.split(',')"
              :key="item"
            >
              <ImagePreview
                :width="50" :height="50"
                :src="item"
                :preview-src-list="[item]"/>
            </template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="产品退款类型" align="center" prop="productRefundType" width="180" />
      <el-table-column label="是否修剪" align="center" prop="isTrim" />
      <el-table-column label="退货地址" align="center" prop="refundAddress" />
      <el-table-column label="收款人开户行" align="center" prop="openingBank" width="180" />
      <el-table-column label="银行卡号" align="center" prop="cardNo" />
      <el-table-column label="收款人姓名" align="center" prop="payeeName" width="180" />
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column label="客户手机号" align="center" prop="memberPhone" width="180" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :options="sys_approval_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditName" />
<!--      <el-table-column label="审批单号" align="center" prop="auditNo" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:bokaRefundOrder:edit']">修改</el-button>-->
<!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:bokaRefundOrder:remove']">删除</el-button>-->
<!--          </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改博卡退款审核记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="bokaRefundOrderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人名称" prop="applicantName">
          <el-input v-model="form.applicantName" placeholder="请输入申请人名称" />
        </el-form-item>
        <el-form-item label="申请人id" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人id" />
        </el-form-item>
        <el-form-item label="军区" prop="militaryArea">
          <el-input v-model="form.militaryArea" placeholder="请输入军区" />
        </el-form-item>
        <el-form-item label="门店" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入门店" />
        </el-form-item>
        <el-form-item label="老师名字" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入老师名字" />
        </el-form-item>
        <el-form-item label="客户名字" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入客户名字" />
        </el-form-item>
        <el-form-item label="会员卡号" prop="memberCardNumber">
          <el-input v-model="form.memberCardNumber" placeholder="请输入会员卡号" />
        </el-form-item>
        <el-form-item label="商品分类" prop="productCategory">
          <el-input v-model="form.productCategory" placeholder="请输入商品分类" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品价格" prop="productPrice">
          <el-input v-model="form.productPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="开单时间" prop="billDate">
          <el-date-picker clearable
            v-model="form.billDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入退款原因" />
        </el-form-item>
        <el-form-item label="单据照片" prop="billImg">
          <el-input v-model="form.billImg" placeholder="请输入单据照片" />
        </el-form-item>
        <el-form-item label="是否修剪" prop="isTrim">
          <el-input v-model="form.isTrim" placeholder="请输入是否修剪" />
        </el-form-item>
        <el-form-item label="退货地址" prop="refundAddress">
          <el-input v-model="form.refundAddress" placeholder="请输入退货地址" />
        </el-form-item>
        <el-form-item label="收款人开户行" prop="openingBank">
          <el-input v-model="form.openingBank" placeholder="请输入收款人开户行" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="收款人姓名" prop="payeeName">
          <el-input v-model="form.payeeName" placeholder="请输入收款人姓名" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input v-model="form.refundPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="客户手机号" prop="memberPhone">
          <el-input v-model="form.memberPhone" placeholder="请输入客户手机号" />
        </el-form-item>
        <el-form-item label="审核人" prop="auditName">
          <el-input v-model="form.auditName" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审批单号" prop="auditNo">
          <el-input v-model="form.auditNo" placeholder="请输入审批单号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BokaRefundOrder">
import { listBokaRefundOrder, getBokaRefundOrder, delBokaRefundOrder, addBokaRefundOrder, updateBokaRefundOrder } from "@/api/business/bokaRefundOrder";

const { proxy } = getCurrentInstance();
const { sys_approval_status } = proxy.useDict("sys_approval_status");
const bokaRefundOrderList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applicantName: undefined,
    applicantId: undefined,
    militaryArea: undefined,
    shopName: undefined,
    teacherName: undefined,
    memberName: undefined,
    memberCardNumber: undefined,
    productCategory: undefined,
    productName: undefined,
    productPrice: undefined,
    billDate: undefined,
    refundReason: undefined,
    billImg: undefined,
    productRefundType: undefined,
    isTrim: undefined,
    refundAddress: undefined,
    openingBank: undefined,
    cardNo: undefined,
    payeeName: undefined,
    refundPrice: undefined,
    memberPhone: undefined,
    auditStatus: undefined,
    auditName: undefined,
    auditNo: undefined,
    memberCardNumber: undefined
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    applicantName: [
      { required: true, message: "申请人名称不能为空", trigger: "blur" }
    ],
    applicantId: [
      { required: true, message: "申请人id不能为空", trigger: "blur" }
    ],
    militaryArea: [
      { required: true, message: "军区不能为空", trigger: "blur" }
    ],
    shopName: [
      { required: true, message: "门店不能为空", trigger: "blur" }
    ],
    teacherName: [
      { required: true, message: "老师名字不能为空", trigger: "blur" }
    ],
    memberName: [
      { required: true, message: "客户名字不能为空", trigger: "blur" }
    ],
    memberCardNumber: [
      { required: true, message: "会员卡号不能为空", trigger: "blur" }
    ],
    productCategory: [
      { required: true, message: "商品分类不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    productPrice: [
      { required: true, message: "商品价格不能为空", trigger: "blur" }
    ],
    billDate: [
      { required: true, message: "开单时间不能为空", trigger: "blur" }
    ],
    refundReason: [
      { required: true, message: "退款原因不能为空", trigger: "blur" }
    ],
    billImg: [
      { required: true, message: "单据照片不能为空", trigger: "blur" }
    ],
    productRefundType: [
      { required: true, message: "产品退款类型不能为空", trigger: "change" }
    ],
    isTrim: [
      { required: true, message: "是否修剪不能为空", trigger: "blur" }
    ],
    refundAddress: [
      { required: true, message: "退货地址不能为空", trigger: "blur" }
    ],
    openingBank: [
      { required: true, message: "收款人开户行不能为空", trigger: "blur" }
    ],
    cardNo: [
      { required: true, message: "银行卡号不能为空", trigger: "blur" }
    ],
    payeeName: [
      { required: true, message: "收款人姓名不能为空", trigger: "blur" }
    ],
    refundPrice: [
      { required: true, message: "退款金额不能为空", trigger: "blur" }
    ],
    memberPhone: [
      { required: true, message: "客户手机号不能为空", trigger: "blur" }
    ],
    auditStatus: [
      { required: true, message: "审核状态不能为空", trigger: "change" }
    ],
    auditName: [
      { required: true, message: "审核人不能为空", trigger: "blur" }
    ],
    auditNo: [
      { required: true, message: "审批单号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询博卡退款审核记录列表 */
function getList() {
  loading.value = true;
  listBokaRefundOrder(queryParams.value).then(response => {
    bokaRefundOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    applicantName: null,
    applicantId: null,
    militaryArea: null,
    shopName: null,
    teacherName: null,
    memberName: null,
    memberCardNumber: null,
    productCategory: null,
    productName: null,
    productPrice: null,
    billDate: null,
    refundReason: null,
    billImg: null,
    productRefundType: null,
    isTrim: null,
    refundAddress: null,
    openingBank: null,
    cardNo: null,
    payeeName: null,
    refundPrice: null,
    memberPhone: null,
    auditStatus: null,
    auditName: null,
    auditNo: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  };
  proxy.resetForm("bokaRefundOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加博卡退款审核记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getBokaRefundOrder(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改博卡退款审核记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bokaRefundOrderRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateBokaRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addBokaRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除博卡退款审核记录编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delBokaRefundOrder(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/bokaRefundOrder/export', {
    ...queryParams.value
  }, `bokaRefundOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
